package com.sanpu.kxe.business.card.presenter;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.business.card.contract.CardConfig;
import com.sanpu.kxe.business.card.contract.UpdateBankTelContract;
import com.sanpu.kxe.business.card.model.CardModel;
import com.sanpu.kxe.utils.ToastUtil;
import com.sanpu.kxe.utils.VerifyUtil;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：修改银行卡预留手机号
 * Created by 薛勇军 on 2018/7/25.
 */

public class UpdateBankTelPresenter implements UpdateBankTelContract.Presenter {

    private UpdateBankTelContract.View mView;

    private CompositeSubscription mSubscription;

    private BankInfoOutput infoOutput = null;

    public UpdateBankTelPresenter(UpdateBankTelContract.View mView, BankInfoOutput infoOutput) {
        this.mView = mView;
        this.infoOutput = infoOutput;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void onCommit(final String rsdTel) {
        if (TextUtils.isEmpty(rsdTel)) {
            ToastUtil.showTextViewPrompt("预留手机号不能为空！");
            return;
        }
        if (VerifyUtil.checkPhoneNumber(rsdTel)) {
            ToastUtil.showTextViewPrompt("预留手机号码不正确！");
            return;
        }
        if (infoOutput == null) {
            ToastUtil.showTextViewPrompt("银行卡信息有误，请返回重新选择！");
            return;
        }
        mView.showLoadingDialog(true);
        Subscription subscription = CardModel.getInstance().updateBankRsdTel(
                infoOutput.getMerchantCode(), infoOutput.getBankNo(), rsdTel)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(String s) {
                        ToastUtil.showTextViewPrompt("修改银行卡预留手机号成功!");
                        Intent intent = new Intent();
                        intent.putExtra(CardConfig.INTENT_RSDTEL, rsdTel);
                        Activity activity = (Activity) mView.getContext();
                        activity.setResult(Activity.RESULT_OK, intent);
                        activity.finish();
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
